﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace KUMC.Neurology.SomatoTopo
{
    [Serializable]
    public class Layer
    {
        /*
         * Member Variable
         * */
        private Bitmap bitMap = null;

        /*
         * Constructor
         * */
        public Layer(string filename)
        {
            this.bitMap = new Bitmap(Image.FromFile(filename));
        }
        public Layer(Image image)
        {
            this.bitMap = new Bitmap(image);
        }
        public Layer(Bitmap bitMap)
        {
            this.bitMap = bitMap;
        }
        public Layer(Size size)
        {
            this.bitMap = new Bitmap(size.Width, size.Height);
        }
        public Layer(int width, int height)
        {
            this.bitMap = new Bitmap(width, height);
        }

        /*
         * Methods
         * */
        protected Graphics CreateG()
        {
            try
            {
                Graphics g = Graphics.FromImage(this.bitMap);
                return g;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Bitmap ZoomInOut(double scale)
        {
            return new Bitmap(this.bitMap, (int)(this.bitMap.Width * scale),
                                                    (int)(this.bitMap.Height * scale));            
        }
        public virtual void Clear()
        {
            this.CreateG().Clear(Color.Transparent);
        }
        public virtual void Clear(Color color)
        {
            this.CreateG().Clear(color);
        }
        public void FillRegion(Point[] points)
        {
            try
            {
                if (points.Length > 1)
                {
                    // Brush coding
                    Brush brush = Brushes.Red;
                    
                    this.CreateG().FillPolygon(brush, points);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void FillRegion(Point[] points, int code)
        {
            try
            {
                if (points.Length > 1)
                {
                    //ColorPalette cp = GetGrayScalePalette();
                    //SolidBrush brush = new SolidBrush(cp.Entries[code]);

                    // Brush coding
                    
                    SolidBrush brush = new SolidBrush(Color.FromArgb(64, Color.FromArgb(code)));
                    this.CreateG().FillPolygon(brush, points);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void DrawString(string str, Font font)
        {
            try
            {
                this.CreateG().DrawString(str, font, Brushes.Black, 0, 0);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // 16 pallet make
        public static ColorPalette GetScalePalette()
        {
            Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format32bppArgb);

            ColorPalette palette = bmp.Palette;

            Color[] entries = palette.Entries;

            for (int i = 0; i < 16; i++)
            {
                entries[i] = Color.FromArgb(255, 255 - (i * 16), i * 16, 0);
            }

            return palette;
        }
        /*
         * Getter
         * */
        public Bitmap Bitmap { get { return this.bitMap; } }
        public Size Size { get { return this.Bitmap.Size; } }
    }
}
